using System.Collections;
using Concurso;
using Concurso.Clases;
using FunkeLab;

namespace ConcursoCV.UI.WebUserControls.Concurso
{
	using System;
	using System.Data;
	using System.Drawing;
	using System.Web;
	using System.Web.UI.WebControls;
	using System.Web.UI.HtmlControls;

	/// <summary>
	///		Summary description for ControladorConcurso.
	/// </summary>
	public class ControladorConcurso : System.Web.UI.UserControl
	{
		protected System.Web.UI.WebControls.Button btnVolver;
		protected WebUserControls.Concurso.ABMAntecedenteConcurso ucABMAntecedenteConcurso;
		protected WebUserControls.Concurso.ArbolConcurso ucArbolConcurso;
		protected WebUserControls.Concurso.AyudaConcurso ucAyudaConcurso;
		
		private const string pidListadoConcursos = "006";

		private void Page_Load(object sender, System.EventArgs e)
		{
			if(!this.IsPostBack)
			{
				this.MostrarAyuda();	
			}
		}

		private void RedirigirAListadoConcursos(string pid)
		{
			string url = "Default.aspx?pid=" + pid;
			Server.Transfer(url);
		}

		private void MostrarAyuda()
		{
			MostrarControles(false,true);
		}
		private void MostrarDetalle()
		{
			MostrarControles(true,false);
		}
		private void MostrarControles( bool bDetalle, bool bAyuda )
		{
			ucAyudaConcurso.Visible = bAyuda;
			ucABMAntecedenteConcurso.Visible = bDetalle;			
		}

		private void HabilitarDeshabilitarAgregarYModificar(bool enabled)
		{
			this.ucArbolConcurso.ModificarEnabled = enabled;
		}

		#region - properties -

		public string SelectedNode 
		{
			get{ return  (string) ViewState["_selectedNode"];}	
			set{ ViewState["_selectedNode"] = value;}
		}

		public ArrayList SelectedPathNode 
		{
			get{ return (ArrayList) ViewState["_pathNode"];}	
			set{ ViewState["_pathNode"] = value;}
		}

		public object OrgNodeId 
		{
			get{ return ViewState["_orgNodeId"];}	
			set{ ViewState["_orgNodeId"] = value;}
		}

		public string SelectedPath 
		{
			get{ return (string) ViewState["_selectedPath"];}	
			set{ ViewState["_selectedPath"] = value;}
		}

		#endregion - properties -

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			MyInitializeComponent();
			base.OnInit(e);
		}
		
		private void MyInitializeComponent()
		{
			// Arbol 
			this.ucArbolConcurso.Modificar +=new EventHandler(ucArbolConcurso_Modificar);
			//
			this.ucArbolConcurso.TreeViewNodeClick +=new EventHandler(ucArbolConcurso_TreeViewNodeClick);
			this.ucArbolConcurso.TreeViewOnNodeClose +=new EventHandler(ucArbolConcurso_TreeViewOnNodeClose);
			// ABM AntecedenteMetadato
			this.ucABMAntecedenteConcurso.Cancelar +=new EventHandler(ucABMAntecedenteConcurso_Cancelar);
		}
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.btnVolver.Click += new System.EventHandler(this.btnVolver_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void ucArbolConcurso_TreeViewNodeClick(object sender, EventArgs e)
		{
			MyTreeViewItem item = ((TreeViewEventArgs) e).Node;
			this.SelectedNode = item.Value;
			if(item != null)
			{
				if(item.Value == "ROOT")
				{
					MostrarAyuda();
				}
				else
				{
					ucABMAntecedenteConcurso.PageMode = PageMode.EDIT;
					ucABMAntecedenteConcurso.CargarAntecedenteAControl(item.Value);
					MostrarDetalle();
				}
			}
		}

		private void ucArbolConcurso_TreeViewOnNodeClose(object sender, EventArgs e)
		{

		}
		private void ucArbolConcurso_Modificar(object sender, EventArgs e)
		{
			HabilitarDeshabilitarAgregarYModificar(false);
			this.ucABMAntecedenteConcurso.PageMode = PageMode.EDIT;
			this.ucABMAntecedenteConcurso.Modificacion();
			MostrarDetalle();
		}

		private void ucABMAntecedenteConcurso_Cancelar(object sender, EventArgs e)
		{
			if(this.SelectedNode != null)
			{
				if(this.SelectedNode == "ROOT")
				{
					MostrarAyuda();
				}
				else
				{
					ucABMAntecedenteConcurso.PageMode = PageMode.EDIT;
					ucABMAntecedenteConcurso.CargarAntecedenteAControl(this.SelectedNode);
					MostrarDetalle();
				}
			}			
		}

		private void btnVolver_Click(object sender, System.EventArgs e)
		{
			RedirigirAListadoConcursos(pidListadoConcursos);
		}
	}
}
